package com.yita.fruit.dao.Impl;

import com.yita.fruit.dao.FruitDAO;
import com.yita.fruit.pojo.Fruit;
import com.yita.myssm.basedao.BaseDAO;

import java.util.List;

public class FruitDaoImpl extends BaseDAO<Fruit> implements FruitDAO {

    @Override
    public List<Fruit> getFruitList(Integer page, Integer rows, String keyword) {
        if (page==null||page<=0){
            page=1;
        }
        if (rows==null||rows<=0){
            rows=5;
        }
        String sql = "select * from t_fruit where fname like ? or remark like ? limit ?,?";
        return this.executeQuery(sql, "%"+keyword+"%","%"+keyword+"%",(page-1)*rows,rows);
    }

    @Override
    public boolean addFruit(Fruit fruit) {
        String sql="insert into t_fruit(fid,fname,price,fcount,remark) values(0,?,?,?,?)";
        return executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark())>0;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {
        return false;
    }

    @Override
    public Fruit getFruitByFname(String fname) {
        return null;
    }

    @Override
    public boolean delFruitById(String fid) {
        String sql="delete from t_fruit where fid=?";
        return executeUpdate(sql,fid)>0;
    }

    @Override
    public Fruit getFruitById(String fid) {
        String sql = "select*from t_fruit where fid=?";
        List<Fruit> fruits = executeQuery(sql, fid);
        if (fruits != null && fruits.size() > 0) {
            return fruits.get(0);
        }
        return null;
    }

    @Override
    public boolean updateFruitById(Fruit fruit) {
        String sql="update t_fruit set fname=?,price=?,fcount=?,remark=? where fid=?";
        return executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark(),fruit.getFid())>0;
    }

    @Override
    public int getFruitCount(String keyword) {
        String sql = "select count(*) from t_fruit where fname like ? or remark like ?";
        Long aLong = (Long) executeComplexQuery(sql,"%"+keyword+"%","%"+keyword+"%").get(0).get("count(*)");
        return aLong.intValue();
    }
}
